home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / MPW / Examples / CExamples / Memory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-03  |  5.7 KB  |  242 lines  |  [TEXT/MPS ]

  1. /*
  2.  File Memory.c
  3.  Copyright Apple Computer, Inc. 1985-1987, 1995
  4.  All rights reserved.
  5.  
  6.  * Memory - report the amount of free space in the
  7.  * application and system heap, and on the boot volume.
  8.  *
  9.  * This is the sample C desk accessory.  The desk accessory does not
  10.  * use any global variables.  Instead, it allocates a handle to a
  11.  * structure that holds some "global" variables.  This sample program
  12.  * could be written without having to use this structure, but then it
  13.  * wouldn't be as informative...
  14.  */
  15. #include    <Types.h>
  16. #include    <Devices.h>
  17. #include    <Fonts.h>
  18. #include    <NumberFormatting.h>
  19. #include    <TextUtils.h>
  20. #include    <Windows.h>
  21.  
  22.  
  23. /*
  24.  * Macro to compute owned resource id
  25.  */
  26.  
  27. #define OWNEDRSRCID(id)    (0xC000 | (((-(id)) - 1) << 5))
  28.  
  29. /*
  30.  * String constant indexes for STR# resource
  31.  */
  32.  
  33. #define    APPHEAP        1
  34. #define    SYSHEAP        2
  35. #define    DISK        3
  36. #define    FREEON        4
  37.  
  38. #define    ACCEVENT    64
  39. #define    ACCRUN        65
  40.  
  41. /* This structure type holds the global variables used by this desk accessory */
  42.  
  43. typedef struct {
  44.     int     rsrcID;         /* Computed rsrc id of STR# and WIND resources */
  45.     Str255    strBuf;            /* Buffer to read strings into */
  46. } Globals;
  47.  
  48. /*
  49.  * Forward declarations
  50.  */
  51. static void doCtlEvent(register EventRecord *theEvent, Globals *globals);
  52. static void doPeriodic(DCtlPtr dCtl);
  53. static void drawWindow(WindowPtr window, Globals *globals);
  54.  
  55. pascal short DRVROpen(CntrlParam *ctlPB, DCtlPtr dCtl)
  56. {
  57.     #pragma    unused (ctlPB)
  58.     GrafPtr        savePort;
  59.     WindowPeek    myWindow;
  60.     long        heapGrow;
  61.  
  62.     /*
  63.      * If the windowPtr is non-nil, we already have a window open.
  64.      * This desk accessory ignores multiple opens.
  65.      */
  66.     if (dCtl->dCtlWindow != nil)
  67.         return noErr;
  68.  
  69.     GetPort(&savePort);
  70.     
  71.     /*
  72.      * Get a handle to some storage that will hold our pseudo-global
  73.      * variables.  Save the handle in a location accessible by
  74.      * all the driver routines.
  75.      */
  76.     dCtl->dCtlStorage = NewHandle(sizeof(Globals));
  77.     /*
  78.      * Compute the resource id of the owned 'STR#' resource that
  79.      * contains all of the program's text strings.  The id is saved
  80.      * in one place that can be accessed by all the driver routines.
  81.      */
  82.     ((Globals *)(*dCtl->dCtlStorage))->rsrcID = OWNEDRSRCID(dCtl->dCtlRefNum);
  83.     
  84.     /*
  85.      * wStorage = nil (allocate on the heap)
  86.      * visible = false, behind = -1, goAway = true, refCon = 0
  87.      */
  88.     myWindow = (WindowPeek)GetNewWindow(((Globals *)(*dCtl->dCtlStorage))->rsrcID, nil, (WindowPtr) -1);
  89.     /*
  90.      * Set windowKind to the DA refNum, which is negative.
  91.      */
  92.     myWindow->windowKind = dCtl->dCtlRefNum;
  93.     /*
  94.      * Store the windowPtr in the Device Control Entry
  95.      */
  96.     dCtl->dCtlWindow = (WindowPtr)myWindow;
  97.     /*
  98.      * Now compact the heap in the most violent way.
  99.      * Purge whatever's purgeable.
  100.      */
  101.     (void) MaxMem(&heapGrow);
  102.  
  103.     SetPort(savePort);
  104.     return noErr;
  105. }
  106.  
  107.  
  108.  
  109. pascal short DRVRPrime(CntrlParam *ctlPB, DCtlPtr dCtl)
  110. {
  111.     #pragma    unused (ctlPB, dCtl)
  112.     return noErr;            /* Not used in this desk accessory */
  113. }
  114.  
  115. pascal short DRVRStatus(CntrlParam *ctlPB, DCtlPtr dCtl)
  116. {
  117.     #pragma    unused (ctlPB, dCtl)
  118.     return noErr;            /* Not used in this desk accessory */
  119. }
  120.  
  121. pascal short DRVRControl(CntrlParam *ctlPB, DCtlPtr dCtl)
  122. {
  123.     /*
  124.      * The current grafPort is saved & restored by the Desk Manager
  125.      */
  126.     switch (ctlPB->csCode) {
  127.         case ACCEVENT:                /* accEvent */
  128.             HLock(dCtl->dCtlStorage);    /* Lock handle since it will be dereferenced */
  129.             doCtlEvent( *((EventRecord **) &ctlPB->csParam[0]),
  130.                     (Globals *)(*dCtl->dCtlStorage));
  131.             HUnlock(dCtl->dCtlStorage);
  132.             break;
  133.  
  134.         case ACCRUN:                /* periodicEvent */
  135.             doPeriodic(dCtl);
  136.             break;
  137.  
  138.         default:
  139.             break;
  140.     }
  141.     return 0;
  142. }
  143.  
  144. static void doCtlEvent(register EventRecord *theEvent, Globals *globals)
  145. {
  146.     register    WindowPtr    myWindow;
  147.  
  148.     if (theEvent->what == updateEvt) {
  149.         myWindow = (WindowPtr) theEvent->message;
  150.         SetPort(myWindow);
  151.         BeginUpdate(myWindow);
  152.         drawWindow(myWindow, globals);
  153.         EndUpdate(myWindow);
  154.     }
  155. }
  156.  
  157. static void doPeriodic(DCtlPtr dCtl)
  158. {
  159.     SetPort(dCtl->dCtlWindow);
  160.     HLock(dCtl->dCtlStorage);    /* Lock handle since it will be dereferenced */
  161.     drawWindow(dCtl->dCtlWindow, (Globals *)(*dCtl->dCtlStorage));
  162.     HUnlock(dCtl->dCtlStorage);
  163. }
  164.  
  165. /*
  166.  * Display the contents of the window.
  167.  * The current port is assumed to be set to the window.
  168.  */
  169. static void drawWindow(WindowPtr window, Globals *globals)
  170. {
  171.     THz                 saveZone;
  172.     Str27                volName;
  173.     HVolumeParam        myParamBlk; 
  174.     void                printNum(unsigned long);
  175.     static    StringPtr    text(int index, Globals *globals);
  176.  
  177.     if (window == nil)
  178.         return;        /* "can't happen" */
  179.  
  180.     TextMode(srcCopy);
  181.     TextFont(kFontIDMonaco);
  182.     TextSize(9);
  183.  
  184.     MoveTo(6, 10);
  185.     TextFace(bold);
  186.  
  187.     saveZone = GetZone();
  188.  
  189.     DrawString(text(APPHEAP, globals));
  190.     SetZone(ApplicationZone());
  191.     printNum(FreeMem());
  192.  
  193.     DrawString(text(SYSHEAP, globals));
  194.     SetZone(SystemZone());
  195.     printNum(FreeMem());
  196.  
  197.     SetZone(saveZone);
  198.  
  199.     DrawString(text(DISK, globals));
  200.     myParamBlk.ioNamePtr = volName;
  201.     myParamBlk.ioVRefNum = 0;        /* Boot volume */
  202.     myParamBlk.ioVolIndex = 0;
  203.     (void) PBHGetVInfo((HParmBlkPtr)&myParamBlk, false);
  204.     printNum((unsigned long)myParamBlk.ioVAlBlkSiz * myParamBlk.ioVFrBlk);
  205.  
  206.     DrawString(text(FREEON, globals));
  207.     TextFace(underline);
  208.     DrawString(volName);
  209. }
  210.  
  211. static void printNum(unsigned long num)
  212. {
  213.     unsigned    char    numStr[32];
  214.  
  215.     TextFace(normal);
  216.     NumToString(num, numStr);        /* Its possible that a large unsigned
  217.                                        will come back negative! */
  218.     DrawString(numStr);
  219.     TextFace(bold);
  220. }
  221.  
  222. pascal short DRVRClose(char *ctlPB, DCtlPtr dCtl)
  223. {                        /* Save & Restore current grafPort? */
  224.     #pragma    unused (ctlPB)
  225.     WindowPtr window;
  226.  
  227.     window = (WindowPtr) dCtl->dCtlWindow;
  228.     if ( window != nil) {
  229.         dCtl->dCtlWindow = nil;
  230.         DisposeHandle(dCtl->dCtlStorage);
  231.         DisposeWindow(window);
  232.     }
  233.  
  234.     return 0;
  235. }
  236.  
  237. static StringPtr text(int index, Globals *globals)
  238. {
  239.     GetIndString(globals->strBuf, globals->rsrcID, index);
  240.     return(globals->strBuf);
  241. }
  242.